<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    public function index($status=0)
    {
        $this->load->model('Ad_model');
        $adlist = $this->Ad_model->get_ads_by_code('home_top_banner');
        
        $underBanAdList = $this->Ad_model->get_ads_by_code('home_under_banner');
        
        $btModelAdList = $this->Ad_model->get_ads_by_code('home_between_model');

        $this->load->model('Recommend_model');
        $this->load->model('Userdetail_model');
        $this->load->model('Usernum_model');

        //$arrWhere = array('usertype'=>1,'status<>'=>-1,'userlevel'=>1,'sex'=>2);
        $arrWhere = array('usertype'=>1,'status'=>$this->show_user_status,'userlevel'=>1,'sex'=>2, 'hangup_status'=>$this->show_hangup_status);
        $feild = 'user.id,nickname,userlogo,company,showimg,showimg2,city,user.usertype';
        $rmdlist1 = $this->Recommend_model->get_user_list($arrWhere, $feild, 10);
        if(!empty($rmdlist1)) {
            foreach ($rmdlist1 as  $key =>$rmd_info) {
               $info =  $this->Userdetail_model->get_info_by_id($rmd_info['id']);
               $rmdlist1[$key]['detail_info'] = $info;
            }
        }

        // 女模
        $arrWhere = array('usertype'=>1,'status'=>$this->show_user_status, 'sex'=>2, 'nickname<>'=>'', 'hangup_status'=>$this->show_hangup_status);
        $rmdlist2 = $this->Recommend_model->get_user_list($arrWhere, $feild, 8);
//         echo $this->Recommend_model->db->last_query();die;
        //获取推荐人的详细信息
        if(!empty($rmdlist2)) {
            foreach ($rmdlist2 as  $key =>$rmd_info) {
               $info =  $this->Userdetail_model->get_info_by_userid($rmd_info['id']);
               $numInfo = $this->Usernum_model->get_info_by_userid($rmd_info['id']);
               $rmdlist2[$key]['detail_info'] = $info;
               $rmdlist2[$key]['num_info'] = $numInfo;
            }
        }
        
        $arrWhere = array('usertype'=>1,'status'=>$this->show_user_status,'userlevel'=>1,'sex'=>1, 'hangup_status'=>$this->show_hangup_status);
        $rmdlist3 = $this->Recommend_model->get_user_list($arrWhere, $feild, 10);
        //获取推荐人的详细信息
        if(!empty($rmdlist3)) {
            foreach ($rmdlist3 as  $key =>$rmd_info) {
               $info =  $this->Userdetail_model->get_info_by_id($rmd_info['id']);
               $rmdlist3[$key]['detail_info'] = $info;
            }
        }

        // 男模
        $arrWhere = array('usertype'=>1,'status'=>$this->show_user_status,'sex'=>1,'nickname<>'=>'', 'hangup_status'=>$this->show_hangup_status);
        $rmdlist4 = $this->Recommend_model->get_user_list($arrWhere, $feild, 8);
        //获取推荐人的详细信息
        if(!empty($rmdlist4)) {
            foreach ($rmdlist4 as  $key =>$rmd_info) {
                $info =  $this->Userdetail_model->get_info_by_userid($rmd_info['id']);
//                 echo $this->Userdetail_model->db->last_query()."<br/>";
                $numInfo = $this->Usernum_model->get_info_by_userid($rmd_info['id']);
                $rmdlist4[$key]['num_info'] = $numInfo;
                $rmdlist4[$key]['detail_info'] = $info;
            }
//             die;
        }
        
        //艺人（机构旗下的艺人也显示）
        $arrWhere = array('usertype in (4,5,6)'=>null,'status'=>$this->show_user_status,'nickname<>'=>'','hangup_status'=>$this->show_hangup_status);
        $rmdlist6 = $this->Recommend_model->get_user_list($arrWhere, $feild, 8);
//         echo $this->Recommend_model->db->last_query();die;
        //获取推荐人的详细信息
        if(!empty($rmdlist6)) {
        	$typeArr = _get_config('type');
        	foreach ($rmdlist6 as $key =>$rmd_info) {
        		// 处理类型展示
        		$typeStr = '';
        		$infoArr = array();
        		$info =  $this->Userdetail_model->get_info_by_userid($rmd_info['id']);
        		$insTypeArr = $typeArr[6];
        		
        		if (!empty($info['type'])) {
        			if (strstr($info['type'], ',')) {
        				$infoArr= explode(',', $info['type']);
        			} else {
        				$typeStr = $insTypeArr[$info['type']];
        			}
        		}
        		
        		if ($infoArr) {
        			foreach($infoArr as $it) {
        				$typeStr .= ','.$insTypeArr[$it];
        			}
        		}
        		if (strstr($typeStr, ','))
        		{
        			$type_str = ltrim($typeStr, ',');
        			$type_arr = explode(',', $type_str);
        			$type_arr = array_unique($type_arr);
        			$typeStr = implode(',', $type_arr);
        		}
        		
        		$info['type_str'] = $typeStr;
        		//echo $this->Userdetail_model->db->last_query()."<br/>";
        		$numInfo = $this->Usernum_model->get_info_by_userid($rmd_info['id']);
        		$rmdlist6[$key]['num_info'] = $numInfo;
        		$rmdlist6[$key]['detail_info'] = $info;
        	}
        }

        // 机构
        $arrWhere = array('usertype'=>2,'status'=>$this->show_user_status,'nickname<>'=>'','hangup_status'=>$this->show_hangup_status);
        $rmdlist5 = $this->Recommend_model->get_user_list($arrWhere, $feild, 8);
        $typeArr = _get_config('type');
        $this->load->model('Usernum_model');
        $insTypeArr = $typeArr[2];
        if (!empty($rmdlist5)) {
            foreach($rmdlist5 as $key=>$rmd_info) {
                $typeStr = '';
                $infoArr = array();
                $info =  $this->Userdetail_model->get_info_by_userid($rmd_info['id']);
                if (!empty($info['type'])) {
                    if (strstr($info['type'], ',')) {
                        $infoArr= explode(',', $info['type']);
                    } else {
                        $typeStr = $insTypeArr[$info['type']];
                    }
                }
                
                if (!empty($infoArr)) {
                    foreach($infoArr as $it) {
                        $typeStr .= ','.$insTypeArr[$it];
                    }
                }
                
                $info['type_str'] = ltrim($typeStr, ',');
                
                $rmdlist5[$key]['detail_info'] = $info;
                
                //获取点击量
                $numInfo = $this->Usernum_model->get_info_by_userid($rmd_info['id']);
                $rmdlist5[$key]['num_info'] = $numInfo;
            }
        }
        

        // 通告
        $this->load->model('Recommend_model');
        $fields = 'activity.id,title,img,summary,workfee,activity.addtime,begtime,inendtime,place,category,type,innum,innumfake,visitnum';
        $arrWhere = array('display'=>1, 'status'=>1);
        $actlist1 = $this->Recommend_model->get_act_list($arrWhere, $fields, 6);
        if (!empty($actlist1)) {
            foreach ($actlist1 as $k=>$n) {
                $actlist1[$k]['w_time'] = $n['begtime']&&is_numeric($n['begtime']) ? (date('Y-m-d', (int)$n['begtime'])) : ''; 
                $actlist1[$k]['endtime'] = date('Y-m-d', $n['inendtime']);
            }
        }
        
        $result = array(
                'adlist' => $adlist,
                'rmdlist1' => $underBanAdList,
                'rmdlist2' => $rmdlist2,
                'rmdlist3' => $btModelAdList,
                'rmdlist4' => $rmdlist4,
                'rmdlist5' => $rmdlist5,
        		'rmdlist6' => $rmdlist6,
                'actlist1' => $actlist1,
        		'status' => $status,
                );
        if ($status)
        {
        	$result['usertype'] = $this->config->item('usertype_reg');
        }
        
        $this->load->view('home',$result);
    }

/*
	public function model(){
		$type = $this->input->get('type');
		$type = $type==0?1:$type;
		$this->load->model('Recommend_model');
		$arrWhere = array('type'=>$type,'status'=>1,'display'=>1,'img2<>'=>'');
		$actlist = $this->Recommend_model->get_act_list($arrWhere, 'activity.id,title,img2,summary,activity.addtime', 10);
		$oAct = array();
		if(!empty($actlist))
		{
			$oAct = $actlist[0];
			unset($actlist[0]);
		}

		$result = array(
			'actlist' => $actlist,
			'oAct' => $oAct,
			);
		$this->load->view('homemodel',$result);
	}
*/


	public function expired(){
		$this->load->view('homeexpired');
	}
	
	//qq授权后回调
	public function qqback($status=0)
	{
		$result = array('status'=>$status);
		if ($status)
		{
			$result['usertype'] = $this->config->item('usertype_reg');
		}
		$this->load->view('/user/back',$result);
	}
}

